package com.weixin.paleituo.adapter;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.weixin.paleituo.R;
import com.weixin.paleituo.base.BaseRecycleAdapter;
import com.weixin.paleituo.bean.PropertyListBean;
import com.weixin.paleituo.bean.SpecificationListBean;
import com.weixin.paleituo.help.RecycleViewHolder;
import com.weixin.paleituo.help.RecycleViewOnItemOnclickListener;
import com.weixin.paleituo.help.SelectPropertyListener;
import com.weixin.paleituo.util.LogUtils;
import com.weixin.paleituo.util.ToastUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.FlowLayout;

import java.util.List;

/**
 * Author yichao
 * Time  2018/3/25 10:49
 * Dest 商品属性
 */

public class SelectGoodsRecycleAdapter extends BaseRecycleAdapter<SpecificationListBean> {
    private RecycleViewOnItemOnclickListener mRecycleViewOnItemOnclickListener;
    private SelectPropertyListener mSelectPropertyListener;
    private int [] selectIdArr;
    private String TAG="SelectGoodsRecycleAdapter";
    private SelectAttrListener mSelectAttrListener;

    public void setVisible(boolean visible) {
        isVisible = visible;
    }
    public void setSelectIdArr() {
        List<SpecificationListBean> datas=getDatas();
        if (datas!=null && datas.size()>0){
            for (int j=0;j<datas.size();j++){

                SpecificationListBean specificationListBean = datas.get(j);
                List<PropertyListBean> propertyList = specificationListBean.getPropertyList();
                if (propertyList != null && propertyList.size() > 0) {
                    if (selectIdArr==null){
                        selectIdArr=new int[getDatas().size()];
                    }

                    for (int i = 0; i < propertyList.size(); i++) {

                        if (i == 0) {
                            selectIdArr[j]=propertyList.get(i).getId();

                        }

                    }


                }

            }


        }


    }
    private boolean isVisible=false;
    public SelectGoodsRecycleAdapter(Context context, List<SpecificationListBean> datas) {
        super(context, datas, R.layout.item_select_goods_attr_recycle);

    }
    @Override
    protected void setData(final RecycleViewHolder holder, SpecificationListBean specificationListBean, final int position) {
        TextView tvTitle = holder.getItemView(R.id.tv_title_item_select_goods_attr_recycle);
        tvTitle.setText(specificationListBean.getName());
        FlowLayout flowLayout = holder.getItemView(R.id.flowlayout_item_select_goods_attr_recycle);
        if (flowLayout.getChildCount() != 0) {
           flowLayout.removeAllViews();
        }
        List<PropertyListBean> propertyList = specificationListBean.getPropertyList();
        if (propertyList != null && propertyList.size() > 0) {
            if (selectIdArr==null){
            selectIdArr=new int[getDatas().size()];
            }
            final TextView[] textViews = new TextView[propertyList.size()];
            final  int [] propertyId=new int[propertyList.size()];
            for (int i = 0; i < propertyList.size(); i++) {
                TextView textView = (TextView) UIUtils.inflate(mContext, R.layout.layout_goods_attrs_text);
                textViews[i] = textView;
                if (i == 0) {
                    textView.setTextColor(UIUtils.getColor(R.color.c_29CE7E));
                    textView.setBackgroundResource(R.drawable.visiter_houser_shape);
                    selectIdArr[position]=propertyList.get(i).getId();
                    LogUtils.i(TAG,"当前position::"+position);
                } else {
                    textView.setBackgroundResource(R.drawable.no_select_attr_shape);
                    textView.setTextColor(UIUtils.getColor(R.color.c_383838));
                }
                textViews[i].setText(propertyList.get(i).getValue());
                propertyId[i]=propertyList.get(i).getId();
                flowLayout.addView(textView);
            }
            for ( int j=0;j<textViews.length;j++){

                textViews[j].setOnClickListener(new SelectOnClickListener(textViews,j,position,propertyId[j]));
            }

        }



    }
    private class SelectOnClickListener implements View.OnClickListener{
        private TextView[] mTextViews;
        private int selectPosition;
        private int mPosition;
        private int mSelectId;

        public SelectOnClickListener(TextView[] textViews, int selectPosition, int position, int selectId) {
            mTextViews = textViews;
            this.selectPosition = selectPosition;
            mPosition = position;
            mSelectId = selectId;
        }

        @Override
        public void onClick(View v) {
            if (isVisible){

                ToastUtils.makeText("请关闭定制服务");
                return;
            }


            for (int i=0;i<mTextViews.length;i++){
                if (selectPosition==i){
                    mTextViews[i].setTextColor(UIUtils.getColor(R.color.c_29CE7E));
                    mTextViews[i].setBackgroundResource(R.drawable.visiter_houser_shape);
                 selectIdArr[mPosition]=mSelectId;
                }else {
                    mTextViews[i].setBackgroundResource(R.drawable.no_select_attr_shape);
                    mTextViews[i].setTextColor(UIUtils.getColor(R.color.c_383838));
                }
            }
            String idList="";
            for (int i=0;i<selectIdArr.length;i++){
              //  LogUtils.i(TAG,"选择的id  ::"+selectIdArr[i]);
                idList=idList+selectIdArr[i]+"_";
            }
            if (mSelectAttrListener!=null){
                mSelectAttrListener.selectOnClick(idList,selectPosition,mPosition);
            }








        }
    }


public interface SelectAttrListener{
        void selectOnClick(String idList, int selectPosition, int position);

}
public void setSelectAttrListener(SelectAttrListener selectAttrListener){

    mSelectAttrListener = selectAttrListener;
}

}
